//package com.ibank.framework.core.sequence;
//
//import lombok.Data;
//import org.apache.commons.lang3.RandomUtils;
//import org.apache.commons.lang3.StringUtils;
//
//import java.net.Inet4Address;
//import java.net.UnknownHostException;
//import java.time.LocalDateTime;
//
///**

// */
//@Data
//public class TimeSeqIdUtil {
//    private long beginTime = 775940378L; // 起始时间戳 20250203，例如Twitter的Snowflake起始时间
//    private final long workerIdBits = 4L; // 机器ID
//    private final long maxWorkerId = ~(-1L << workerIdBits); // 最大机器ID: 15
//
//    private final long sequenceBits = 19L; // 序列号 524287
//    private final long workerIdShift = sequenceBits; // 机器ID位置
//    private final long timestampLeftShift = sequenceBits + workerIdBits; // 时间戳位置
//    private final long sequenceMask = ~(-1L << sequenceBits); // 序列号复位器
//
//    private long workerId;
//    private long sequence = 524286L;
//    private static TimeSeqIdUtil _self;
//
//    public static TimeSeqIdUtil getInstance() {
//        if (_self == null) {
//            _self = new TimeSeqIdUtil(getWorkId());
//        }
//        return _self;
//    }
//
//    public TimeSeqIdUtil(long workerId) {
//        if (workerId > maxWorkerId || workerId < 0) {
//            throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
//        }
//        _self = this;
//        this.workerId = workerId;
//    }
//
//    public static long generateId() {
//        return TimeSeqIdUtil.getInstance().nextId();
//    }
//
//
//    public synchronized long nextId() {
//        sequence = (sequence + 1) & sequenceMask;
//        if (sequence == 0) {
//            beginTime++;
//            final Long curTime = TimeSeqIdUtil.compressTime(LocalDateTime.now());
//            if (beginTime > curTime) {
//                try {
//                    wait(1000L); // 主要为了减速
//                    beginTime = curTime;
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }
//        System.out.println(beginTime+","+workerId+","+sequence);
//        return (beginTime << timestampLeftShift) |
//                (workerId << workerIdShift) |
//                sequence;
//    }
//
//    protected long tilNextMillis(long lastTimestamp) {
//        long timestamp = timeGen();
//        while (timestamp <= lastTimestamp) {
//            timestamp = timeGen();
//        }
//        return timestamp;
//    }
//
//    protected long timeGen() {
//        return System.currentTimeMillis();
//    }
//
//    private static Long getWorkId(){
//        try {
//            String hostAddress = Inet4Address.getLocalHost().getHostAddress();
//            int[] ints = StringUtils.toCodePoints(hostAddress);
//            int sums = 0;
//            for(int b : ints){
//                sums += b;
//            }
//            return (long)(sums % 256);
//        } catch (UnknownHostException e) {
//            // 如果获取失败，则使用随机数备用
//            return RandomUtils.nextLong(0,255);
//        }
//    }
//
//    public static Long compressTime(LocalDateTime now) {
//            int year = now.getYear();
//            int month = now.getMonthValue();
//            int day = now.getDayOfMonth();
//            int hour = now.getHour();
//            int minute = now.getMinute();
//            int second = now.getSecond();
////            System.out.println(year+"-"+month+"-"+day+"-"+hour+":"+minute+":"+second);
//            // 对时间进行压缩
//            long compressed = (year << 40) | (month << 28) | (day << 23) | (hour << 18) | (minute << 12) | second;
//            return compressed;
//    }
//
//    public static void main(String[] args) throws InterruptedException {
////        TimeSeqIdUtil idGenerator = new TimeSeqIdUtil(0);
////        for (int i = 0; i < 10; i++) {
////            System.out.println(idGenerator.nextId());
////        }
//
////        long workerIdBits = 8L;
////        long maxWorkerId = -1L ^ (-1L << workerIdBits);
////        System.out.println(maxWorkerId);
//
//
////        for (int i = 0; i < 10; i++) {
////            LocalDateTime now = LocalDateTime.now();
////            int year = now.getYear();
////            int month = now.getMonthValue();
////            int day = now.getDayOfMonth();
////            int hour = now.getHour();
////            int minute = now.getMinute();
////            int second = now.getSecond();
////            System.out.println(year+"-"+month+"-"+day+"-"+hour+":"+minute+":"+second);
////            // 对时间进行压缩
////            long compressed = (year << 40) | (month << 28) | (day << 23) | (hour << 18) | (minute << 12) | second;
////            System.out.println(compressed);
////            Thread.sleep(1000L);
////        }
//        new TimeSeqIdUtil(10L);
//        for (int i = 0; i < 10; i++) {
//            System.out.println(TimeSeqIdUtil.generateId());
//
////            Long time = compressTime(LocalDateTime.now());
////            System.out.println(time);
////            Thread.sleep(1000L);
//        }
//
//    }
//}
//
